package com.it.admin.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.admin.AdminPlateService;
import com.it.dao.PlateMapper;
import com.it.pojo.Category;
import com.it.pojo.Plate;
import com.it.pojo.params.BasePage;
import com.it.pojo.vo.category.AdminCategoryVO;
import com.it.pojo.vo.plate.AdminPlateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AdminPlateServiceImpl implements AdminPlateService {

    @Resource
    private PlateMapper plateMapper;

    @Override
    public List<AdminPlateVO> listPlates() {
        List<Plate> plates = plateMapper.selectList(null);
        return copyList(plates);
    }

    @Override
    public int insertPlate(String plateName) {
        Plate plate = new Plate();
        Date date = new Date();
        plate.setPlateName(plateName);
        plate.setGmtCreate(date);
        plate.setGmtModified(date);
        return plateMapper.insert(plate);
    }

    @Override
    public Map<String, Object> listPlates(BasePage basePage) {
        Page<Plate> page = new Page<>(basePage.getPageNum(), basePage.getLimit());
        Page<Plate> platePage = plateMapper.selectPage(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("plateList", page.getRecords());
        return map;
    }

    @Override
    public int deletePlateById(Integer plateId) {
        return plateMapper.deleteById(plateId);
    }

    @Override
    public int updatePlateById(Plate plate) {
        plate.setGmtModified(new Date());
        return plateMapper.updateById(plate);
    }

    @Override
    public String getPlate(Integer plateId) {
        Plate plate = plateMapper.selectById(plateId);
        if (plate != null) {
            return plate.getPlateName();
        }
        return null;
    }

    private AdminPlateVO copy(Plate plate) {
        AdminPlateVO adminPlateVO = new AdminPlateVO();
        BeanUtils.copyProperties(plate, adminPlateVO);
        return adminPlateVO;
    }

    private List<AdminPlateVO> copyList(List<Plate> plateList) {
        List<AdminPlateVO> list = new ArrayList<>();
        for (Plate plate : plateList) {
            list.add(copy(plate));
        }
        return list;
    }
}
